package Application.FirstApplication.manager
{
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	
	import Application.FirstApplication.def.Def;
	import def.XG;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	
	import org.puremvc.as3.patterns.proxy.Proxy;

	public class SourceManager  extends Proxy
	{
		private var m_brLoadConfig : BulkLoader;
		private var m_brLoadSource : BulkLoader;
		
		private var m_peloadingCompleteCallBack : Function;  //预加载回�?xml �?preloading.swf
		private var m_sourceCompleteCallBack : Function;	  //加载游戏资源内容回调 
		
		private var m_gameConfig : XML;
		
		private var m_loaderContext : LoaderContext;

		public function SourceManager()
		{
			m_loaderContext = new LoaderContext;
			m_loaderContext.applicationDomain = ApplicationDomain.currentDomain;
		}
		
		/**先加载配置文件再加载资源文件**/
		public function loadPreloadingSource(callBack : Function = null) : void
		{
			if(callBack != null)
			{
				m_peloadingCompleteCallBack = callBack;
			}
			
			m_brLoadConfig = new BulkLoader("LoadGameConfig");
			
			m_brLoadConfig.add(Def.URL_RefConfig,{id:"SourceConfig"});
			
			m_brLoadConfig.addEventListener(BulkProgressEvent.PROGRESS,OnLoadConfigPregress);
			m_brLoadConfig.addEventListener(BulkProgressEvent.COMPLETE,OnLoadConfigComplete);
			
			m_brLoadConfig.start();
		}
		
		private function OnLoadConfigPregress(evt : BulkProgressEvent) : void
		{
//			sendNotification(Define.LOADCONFIG_PROGRESS,{ bytesLoaded : evt.bytesLoaded , bytesTotal : evt.bytesTotal} );
		}
		
		private function OnLoadConfigComplete(evt : BulkProgressEvent) : void
		{
			m_brLoadConfig.removeEventListener(BulkProgressEvent.PROGRESS,OnLoadConfigPregress);
			m_brLoadConfig.removeEventListener(BulkProgressEvent.COMPLETE,OnLoadConfigComplete);
			
			OnPreLoading(m_gameConfig = m_brLoadConfig.getXML("SourceConfig"));
			
			m_brLoadConfig.clear();
			m_brLoadConfig = null;
		}
		
		/**加载预加载资�?*/
		private function OnPreLoading(configXML : XML) : void
		{	
			m_brLoadSource = new BulkLoader("LoadGameSource");	
			
			m_brLoadSource.addEventListener(BulkProgressEvent.PROGRESS,OnPreloadingPregress);
			m_brLoadSource.addEventListener(BulkProgressEvent.COMPLETE,OnPreloadingComplete);
			
			for each(var item : XML in configXML.elements("preloading"))
			{ 
				m_brLoadSource.add(item.@url.toString() ,{id : item.@id.toString() ,priority : int(item.@priority),context:m_loaderContext } );	
			}
			
			m_brLoadSource.start();
		}
		
		private function OnPreloadingPregress(evt : BulkProgressEvent) : void
		{
			
		}
		
		private function OnPreloadingComplete(evt : BulkProgressEvent) : void
		{
			m_brLoadSource.removeEventListener(BulkProgressEvent.PROGRESS,OnPreloadingPregress);
			m_brLoadSource.removeEventListener(BulkProgressEvent.COMPLETE,OnPreloadingComplete);
			
			if(m_peloadingCompleteCallBack != null)
			{
				m_peloadingCompleteCallBack();
				
				m_peloadingCompleteCallBack = null;
			}
		}
		
		/************************加载游戏资源********************************/
		public function loadGameSource(callBack : Function = null) : void
		{
			if(callBack != null)
			{
				m_sourceCompleteCallBack = callBack;
			}
			
			for each(var item : XML in m_gameConfig.elements("item"))
			{ 
				m_brLoadSource.add(item.@url.toString() ,{id : item.@id.toString() ,priority : int(item.@priority),context:m_loaderContext } );	
			}
			
			m_brLoadSource.addEventListener(BulkProgressEvent.PROGRESS,OnLoadSourcePregress);
			m_brLoadSource.addEventListener(BulkProgressEvent.COMPLETE,OnLoadSourceComplete);
			
			m_brLoadSource.start();
		}
		
		/**加载游戏内容资源**/
		private function OnLoadSourcePregress(evt : BulkProgressEvent) : void
		{
			sendNotification(Def.LOADSOURCE_PROGRESS,{ bytesLoaded : evt.bytesLoaded , bytesTotal : evt.bytesTotal} );
		}
		
		private function OnLoadSourceComplete(evt : BulkProgressEvent) : void
		{
			m_brLoadSource.removeEventListener(BulkProgressEvent.PROGRESS,OnLoadSourcePregress);
			m_brLoadSource.removeEventListener(BulkProgressEvent.COMPLETE,OnLoadSourceComplete);
			
			if(m_sourceCompleteCallBack != null)
			{
				m_sourceCompleteCallBack();
				
				m_sourceCompleteCallBack = null;
			}
		}
		
		/**获取资源**/
		public function getClass( key : String) : Class
		{
			return m_loaderContext.applicationDomain.getDefinition(key) as Class;
		}
		
		public function getXML( key : String ) : XML
		{
			return m_brLoadSource.getXML( key );
		}
		
		public function getMovieClip( key : String ) : MovieClip
		{
			return m_brLoadSource.getMovieClip( key);
		}
	}
}